<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="分类名称" prop="categoryName">
        <el-input
          v-model="queryParams.categoryName"
          placeholder="请输入分类名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="分类编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入分类编码"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd"
          >新增</el-button
        >
        <!-- v-hasPermi="['workflow:category:add']" -->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
        <!-- v-hasPermi="['workflow:category:edit']" -->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-delete"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
        <!-- v-hasPermi="['workflow:category:remove']" -->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          @click="handleExport"
          >导出</el-button
        >
        <!-- v-hasPermi="['workflow:category:export']" -->
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="categoryList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="分类编号" prop="categoryId" v-if="true" />
      <el-table-column label="分类名称" prop="categoryName" />
      <el-table-column label="分类编码" prop="code" />
      <el-table-column label="备注" prop="remark" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="form" :model="formModel" :rules="rules" label-width="80px">
        <el-form-item label="分类名称" prop="categoryName">
          <el-input
            v-model="formModel.categoryName"
            placeholder="请输入分类名称"
          />
        </el-form-item>
        <el-form-item label="分类编码" prop="code">
          <el-input v-model="formModel.code" placeholder="请输入分类编码" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="formModel.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div class="footer text-right">
        <el-button @click="cancel">取 消</el-button>
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="WorkCategory">
import {
  listCategory,
  getCategory,
  delCategory,
  addCategory,
  updateCategory
} from '@/api/workflow/category'
import { reactive, toRefs } from 'vue'
const { proxy } = getCurrentInstance()
const data = reactive({
  // 按钮loading
  buttonLoading: false,
  // 遮罩层
  loading: true,
  // 选中数组
  ids: [],
  // 非单个禁用
  single: true,
  // 非多个禁用
  multiple: true,
  // 显示搜索条件
  showSearch: true,
  // 总条数
  total: 0,
  // 【请填写功能名称】表格数据
  categoryList: [],
  // 弹出层标题
  title: '',
  // 是否显示弹出层
  open: false,
  // 查询参数
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    categoryName: undefined,
    code: undefined
  },
  // 表单参数
  formModel: {},
  // 表单校验
  rules: {
    categoryId: [
      { required: true, message: '分类ID不能为空', trigger: 'blur' }
    ],
    categoryName: [
      { required: true, message: '分类名称不能为空', trigger: 'blur' }
    ],
    code: [{ required: true, message: '分类编码不能为空', trigger: 'blur' }],
    remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }]
  }
})
const {
  buttonLoading,
  loading,
  ids,
  single,
  multiple,
  showSearch,
  total,
  categoryList,
  title,
  open,
  queryParams,
  formModel,
  rules
} = toRefs(data)
/** 查询【请填写功能名称】列表 */
function getList() {
  loading.value = true
  listCategory(queryParams.value).then(response => {
    categoryList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}
// 取消按钮
function cancel() {
  open.value = false
  reset()
}
// 表单重置
function reset() {
  formModel.value = {
    categoryId: undefined,
    categoryName: undefined,
    code: undefined,
    createBy: undefined,
    createTime: undefined,
    updateBy: undefined,
    updateTime: undefined,
    remark: undefined,
    delFlag: undefined
  }
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}
/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    categoryName: undefined,
    code: undefined
  }
  handleQuery()
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.categoryId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}
/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加流程分类'
}
/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset()
  const categoryId = row.categoryId || ids.value
  getCategory(categoryId).then(response => {
    loading.value = false
    formModel.value = response.data
    open.value = true
    title.value = '修改流程分类'
  })
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs['form'].validate(valid => {
    if (valid) {
      buttonLoading.value = true
      if (formModel.value.categoryId != null) {
        updateCategory(formModel.value)
          .then(response => {
            proxy.$modal.msgSuccess('修改成功')
            open.value = false
            getList()
          })
          .finally(() => {
            buttonLoading.value = false
          })
      } else {
        addCategory(formModel.value)
          .then(response => {
            proxy.$modal.msgSuccess('新增成功')
            open.value = false
            getList()
          })
          .finally(() => {
            buttonLoading.value = false
          })
      }
    }
  })
}
/** 删除按钮操作 */
function handleDelete(row) {
  const categoryIds = row.categoryId || ids.value
  proxy.$modal
    .confirm('是否确认删除该数据项？')
    .then(() => {
      loading.value = true
      return delCategory(categoryIds)
    })
    .then(() => {
      loading.value = false
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .finally(() => {
      loading.value = false
    })
}
/** 导出按钮操作 */
function handleExport() {
  proxy.downLoadByUrl(
    'bpmn/workflow/category/export',
    {
      ...queryParams.value
    },
    `category_${new Date().getTime()}.xlsx`
  )
}
getList()
</script>
